package org.jeecgframework.web.service.impl;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.Serializable;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.web.service.ZyInspectionRecordService;
import org.jeecgframework.web.system.pojo.base.ZyFileLogEntity;
import org.jeecgframework.web.system.pojo.base.ZyInspectionRecordEntity;
import org.jeecgframework.web.system.pojo.base.ZyUpInspectionRecordThread;
import org.jeecgframework.web.util.ExcelUtil;
import org.jeecgframework.web.util.ExcelWrite;
import org.jeecgframework.web.util.FileBean;
import org.jeecgframework.web.util.StaticValue;
import org.jeecgframework.web.util.UploadFileUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.fr.report.io.core.ExcelUtils;

@Service("zyInspectionRecordService")
@Transactional
public class ZyInspectionRecordServiceImpl extends CommonServiceImpl implements ZyInspectionRecordService {

	
 	public <T> void delete(T entity) {
 		super.delete(entity);
 		//执行删除操作配置的sql增强
		this.doDelSql((ZyInspectionRecordEntity)entity);
 	}
 	
 	public <T> Serializable save(T entity) {
 		Serializable t = super.save(entity);
 		//执行新增操作配置的sql增强
 		this.doAddSql((ZyInspectionRecordEntity)entity);
 		return t;
 	}
 	
 	public <T> void saveOrUpdate(T entity) {
 		super.saveOrUpdate(entity);
 		//执行更新操作配置的sql增强
 		this.doUpdateSql((ZyInspectionRecordEntity)entity);
 	}
 	
 	/**
	 * 默认按钮-sql增强-新增操作
	 * @param id
	 * @return
	 */
 	public boolean doAddSql(ZyInspectionRecordEntity t){
	 	return true;
 	}
 	/**
	 * 默认按钮-sql增强-更新操作
	 * @param id
	 * @return
	 */
 	public boolean doUpdateSql(ZyInspectionRecordEntity t){
	 	return true;
 	}
 	/**
	 * 默认按钮-sql增强-删除操作
	 * @param id
	 * @return
	 */
 	public boolean doDelSql(ZyInspectionRecordEntity t){
	 	return true;
 	}
 	
 	/**
	 * 替换sql中的变量
	 * @param sql
	 * @return
	 */
 	public String replaceVal(String sql,ZyInspectionRecordEntity t){
 		sql  = sql.replace("#{id}",String.valueOf(t.getId()));
 		sql  = sql.replace("#{spare_sn}",String.valueOf(t.getSpareSn()));
 		sql  = sql.replace("#{plan_id}",String.valueOf(t.getPlanId()));
 		sql  = sql.replace("#{actual_date}",String.valueOf(t.getActualDate()));
 		sql  = sql.replace("#{company}",String.valueOf(t.getCompany()));
 		sql  = sql.replace("#{engineer}",String.valueOf(t.getEngineer()));
 		sql  = sql.replace("#{hosue}",String.valueOf(t.getHouse()));
 		sql  = sql.replace("#{yt_rm_humidity}",String.valueOf(t.getYtRmHumidity()));
 		sql  = sql.replace("#{yt_rm_temperature}",String.valueOf(t.getYtRmTemperature()));
 		sql  = sql.replace("#{lq_coolant_liquid_h}",String.valueOf(t.getLqCoolantLiquidH()));
 		sql  = sql.replace("#{lq_score_coolant}",String.valueOf(t.getLqScoreCoolant()));
 		sql  = sql.replace("#{lq_vent_speed}",String.valueOf(t.getLqVentSpeed()));
 		sql  = sql.replace("#{qj_dust_filter_cl_mp}",String.valueOf(t.getQjDustFilterClMp()));
 		sql  = sql.replace("#{xl_zero_voltage}",String.valueOf(t.getXlZeroVoltage()));
 		sql  = sql.replace("#{xd_run_time}",String.valueOf(t.getXdRunTime()));
 		sql  = sql.replace("#{xd_run_count}",String.valueOf(t.getXdRunCount()));
 		sql  = sql.replace("#{gx_light_path}",String.valueOf(t.getGxLightPath()));
 		sql  = sql.replace("#{yx_light_c}",String.valueOf(t.getYxLightC()));
 		sql  = sql.replace("#{total_run_time}",String.valueOf(t.getTotalRunTime()));
 		sql  = sql.replace("#{after_score}",String.valueOf(t.getAfterScore()));
 		sql  = sql.replace("#{eng_item_id}",String.valueOf(t.getEngItemId()));
 		sql  = sql.replace("#{ti_icp_item_id}",String.valueOf(t.getTiIcpItemId()));
 		sql  = sql.replace("#{lps_item_id}",String.valueOf(t.getLpsItemId()));
 		sql  = sql.replace("#{enigma_item_id}",String.valueOf(t.getEnigmaItemId()));
 		sql  = sql.replace("#{controler_item_id}",String.valueOf(t.getControlerItemId()));
 		sql  = sql.replace("#{smps_item_id}",String.valueOf(t.getSmpsItemId()));
 		sql  = sql.replace("#{server_brand}",String.valueOf(t.getServerBrand()));
 		sql  = sql.replace("#{server_model}",String.valueOf(t.getServerModel()));
 		sql  = sql.replace("#{comprehensive_evaluation}",String.valueOf(t.getComprehensiveEvaluation()));
 		sql  = sql.replace("#{create_name}",String.valueOf(t.getCreateName()));
 		sql  = sql.replace("#{create_by}",String.valueOf(t.getCreateBy()));
 		sql  = sql.replace("#{update_name}",String.valueOf(t.getUpdateName()));
 		sql  = sql.replace("#{update_by}",String.valueOf(t.getUpdateBy()));
 		sql  = sql.replace("#{create_date}",String.valueOf(t.getCreateDate()));
 		sql  = sql.replace("#{update_date}",String.valueOf(t.getUpdateDate()));
 		sql  = sql.replace("#{UUID}",UUID.randomUUID().toString());
 		return sql;
 	}

	@Override
	public void parseExcel(HttpServletRequest req) {
		try {
			String filePath = req.getParameter("filePath");
			List<Map<String, Object>> mapList = ExcelUtil.analyzeExcel(filePath, "1");
			int maxUnit = 2000;//每N 条 启动一个线程
    		int countRow = mapList.size() - 1;
    		int threadC = countRow / maxUnit;
    		int points = countRow % maxUnit;
    		if(threadC > 0){
    			if(points > 0){
    				threadC = threadC + 1;
    			}
    		}else{
    			threadC = 1;
    		}
    		System.out.println("启动线程数量:" + threadC);
    		for (int i = 0; i < threadC; i++) {
    			int beginR = i == 0 ? 1 : i * maxUnit + 1;
				int endR = i == (threadC -1 ) ? countRow : beginR + maxUnit - 1 ;
				ZyUpInspectionRecordThread upIrThread = new ZyUpInspectionRecordThread(beginR, endR, mapList);
				upIrThread.start();
			}
//			XSSFWorkbook hwk = ExcelUtil.analyzeExcel2(new FileInputStream(new File(filePath)));
//			analysisExcelAndSave(hwk);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
//	
//	private void analysisExcelAndSave(XSSFWorkbook xssfWb){
//		try {
//			XSSFSheet sheet = xssfWb.getSheetAt(0);
//			int maxUnit = 500;//每N 条 启动一个线程
//    		int countRow = sheet.getPhysicalNumberOfRows();
//    		int threadC = countRow / maxUnit;
//    		int points = countRow % maxUnit;
//    		if(threadC > 0){
//    			if(points > 0){
//    				threadC = threadC + 1;
//    			}
//    		}else{
//    			threadC = 1;
//    		}
//    		System.out.println("启动线程数量:" + threadC);
//    		for (int i = 0; i < threadC; i++) {
//    			int beginR = i == 0 ? 1 : i * maxUnit - 1;
//				int endR = i == 0 ? maxUnit - 2 : beginR + maxUnit - 1;
//				ZyUpInspectionRecordThread upIrThread = new ZyUpInspectionRecordThread(beginR, endR, sheet);
//				upIrThread.start();
//			}
//		} catch (Exception e) {
//			e.printStackTrace();
//		} 
//	}
//	
//	private List<ZyInspectionRecordEntity> parseExcelToList(XSSFWorkbook hwb) {
//
//		List<ZyInspectionRecordEntity> result = new ArrayList<ZyInspectionRecordEntity>();
//		 //日期格式化   
//        DateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
//		// 得到第一个工作表
//		XSSFSheet sheet = hwb.getSheetAt(0);
//		XSSFRow row = null;
//		// 遍历该表格中所有的工作表，i表示工作表的数量 getNumberOfSheets表示工作表的总数
//		for (int i = 0; i < hwb.getNumberOfSheets(); i++) {
//			sheet = hwb.getSheetAt(i);
//			ZyInspectionRecordEntity zyInspectionRecord;
//			// 遍历该行所有的行,j表示行数 getPhysicalNumberOfRows行的总数
//			for (int j = 1; j < sheet.getPhysicalNumberOfRows(); j++) {
//				row = sheet.getRow(j);
//				zyInspectionRecord = new ZyInspectionRecordEntity();
//				zyInspectionRecord.setSpareSn(ExcelUtil.getCellValue(row.getCell(0)));
//				zyInspectionRecord.setPlanId(ExcelUtil.getCellValue(row.getCell(1)));
//				try {
//					zyInspectionRecord.setActualDate(ft.parse(ExcelUtil.getCellValue(row.getCell(2))));
//				} catch (ParseException e) {
//					e.printStackTrace();
//				}
//				zyInspectionRecord.setCompany(ExcelUtil.getCellValue(row.getCell(3))); // xssfCell.getRichStringCellValue().getString();  
//				zyInspectionRecord.setEngineer(ExcelUtil.getCellValue(row.getCell(4)));
//				zyInspectionRecord.setHouse(ExcelUtil.getCellValue(row.getCell(5)));
////				zyInspectionRecord.setYtRmTemperature(Double.valueOf(ExcelUtil.getCellValue(row.getCell(6)).endsWith("")?"0":ExcelUtil.getCellValue(row.getCell(6))));
//				zyInspectionRecord.setYtRmTemperature(getCellStringValue(row.getCell(6)));
////				zyInspectionRecord.setYtRmHumidity(Double.valueOf(ExcelUtil.getCellValue(row.getCell(7)).equals("")?"0":ExcelUtil.getCellValue(row.getCell(7))));
//				zyInspectionRecord.setYtRmHumidity(getCellStringValue(row.getCell(7)));
////				zyInspectionRecord.setLqCoolantLiquidH(Double.valueOf(ExcelUtil.getCellValue(row.getCell(8)).equals("")?"0":ExcelUtil.getCellValue(row.getCell(8))));
//				zyInspectionRecord.setLqCoolantLiquidH(getCellStringValue(row.getCell(8))); 
//				zyInspectionRecord.setLqScoreCoolant(getCellStringValue(row.getCell(9)));
////				zyInspectionRecord.setLqVentSpeed(Double.valueOf(ExcelUtil.getCellValue(row.getCell(10)).equals("")?"0":ExcelUtil.getCellValue(row.getCell(10))));
//				zyInspectionRecord.setLqVentSpeed(getCellStringValue(row.getCell(10)));
//				zyInspectionRecord.setQjDustFilterClMp(ExcelUtil.getCellValue(row.getCell(11)));
////				zyInspectionRecord.setXlZeroVoltage(Double.valueOf(ExcelUtil.getCellValue(row.getCell(12)).equals("")?"0":ExcelUtil.getCellValue(row.getCell(12))));
//				zyInspectionRecord.setXlZeroVoltage(getCellStringValue(row.getCell(12)));
////				zyInspectionRecord.setXdRunTime(Double.valueOf(ExcelUtil.getCellValue(row.getCell(13)).equals("")?"0":ExcelUtil.getCellValue(row.getCell(13))));
//				zyInspectionRecord.setXdRunTime(getCellStringValue(row.getCell(13)));
////				zyInspectionRecord.setXdRunCount(Double.valueOf(ExcelUtil.getCellValue(row.getCell(14)).equals("")?"0":ExcelUtil.getCellValue(row.getCell(14))));
//				zyInspectionRecord.setXdRunCount(ExcelUtil.getCellValue(row.getCell(14)));
//				zyInspectionRecord.setGxLightPath(ExcelUtil.getCellValue(row.getCell(15)));
////				zyInspectionRecord.setYxLightC(Double.valueOf(ExcelUtil.getCellValue(row.getCell(16)).equals("")?"0":ExcelUtil.getCellValue(row.getCell(16))));
//				zyInspectionRecord.setYxLightC(getCellStringValue(row.getCell(16)));
//				zyInspectionRecord.setTotalRunTime(ExcelUtil.getCellValue(row.getCell(17)));
//				zyInspectionRecord.setAfterScore(ExcelUtil.getCellValue(row.getCell(18)));
//				zyInspectionRecord.setEngItemId(ExcelUtil.getCellValue(row.getCell(19)));
//				zyInspectionRecord.setTiIcpItemId(ExcelUtil.getCellValue(row.getCell(20)));
//				zyInspectionRecord.setLpsItemId(ExcelUtil.getCellValue(row.getCell(21)));
//				zyInspectionRecord.setEnigmaItemId(ExcelUtil.getCellValue(row.getCell(22)));
//				zyInspectionRecord.setControlerItemId(ExcelUtil.getCellValue(row.getCell(23)));
//				zyInspectionRecord.setSmpsItemId(ExcelUtil.getCellValue(row.getCell(24)));
//				zyInspectionRecord.setServerBrand(ExcelUtil.getCellValue(row.getCell(25)));
//				zyInspectionRecord.setServerModel(ExcelUtil.getCellValue(row.getCell(26)));
//				zyInspectionRecord.setComprehensiveEvaluation(ExcelUtil.getCellValue(row.getCell(27)));
//				result.add(zyInspectionRecord);
//			}
//		}
//		return result;
//	}
	
}